package com.example.controller;


import com.example.common.result.Result;
import com.example.entity.dto.SelectCustomerCoptcDto;
import com.example.entity.vo.CustomerWorkShopDetailVo;
import com.example.entity.vo.SelectCustomerCoptcDetaiVo;
import com.example.entity.vo.SelectCustomerCoptcDetailVo;
import com.example.entity.vo.Taskprogressvo;
import com.example.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/customer")
@Api(tags = "客户订单相关接口")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @ApiOperation(value = "销售订单未交付看板")
    @PostMapping("/selectCustomerCoptcDetail")
    public Result<SelectCustomerCoptcDetailVo> selectCustomerCoptcDetail(
            @RequestBody SelectCustomerCoptcDto selectCustomerCoptcDto
            ){
        SelectCustomerCoptcDetailVo vo=new SelectCustomerCoptcDetailVo();
        ArrayList<String> list1 = new ArrayList<>();
        //如果TC004中有多条数据就根据逗号截取
        if(selectCustomerCoptcDto.getTc004()!=null && !selectCustomerCoptcDto.getTc004().isEmpty()){
            String[] strings=selectCustomerCoptcDto.getTc004().split(",");
            list1.addAll(Arrays.asList(strings));
        } else {
            list1=null;
        }
        //查询出数据
        List<SelectCustomerCoptcDetaiVo> sccdv = customerService.selectCustomerCoptcDetail(list1,
                selectCustomerCoptcDto.getTd004()
                ,selectCustomerCoptcDto.getTd013()
                ,selectCustomerCoptcDto.getTc012()
                ,selectCustomerCoptcDto.getTd005()
                ,selectCustomerCoptcDto.getTc003()
                ,selectCustomerCoptcDto.getTd006()
                ,selectCustomerCoptcDto.getTd008()
                ,selectCustomerCoptcDto.getTd009()
                ,selectCustomerCoptcDto.getTd010()
        );
        vo.setData(sccdv);

        //循环查询的所有数据
        for (SelectCustomerCoptcDetaiVo detaiVo : sccdv) {
            //对应字段添加限制小数点后几位
            BigDecimal bigDecimal = new BigDecimal(detaiVo.getTa015());
            BigDecimal roundedDecimal = bigDecimal.setScale(0, RoundingMode.HALF_UP);//参数1：限制小数点几位。
            detaiVo.setTa015(""+roundedDecimal);                                                //参数2：四舍五入
            BigDecimal bigDecimal1 = new BigDecimal(detaiVo.getTa017());
            BigDecimal roundedDecimal1 = bigDecimal1.setScale(0, RoundingMode.HALF_UP);
            detaiVo.setTa017(""+roundedDecimal1);
            BigDecimal bigDecimal2 = new BigDecimal(detaiVo.getTd009());
            BigDecimal roudedDecimal2 = bigDecimal2.setScale(2, RoundingMode.HALF_UP);
            detaiVo.setTd009(""+roudedDecimal2);
            BigDecimal bigDecimal3 = new BigDecimal(detaiVo.getTd008());
            BigDecimal roudedDecumal3 = bigDecimal3.setScale(2, RoundingMode.HALF_UP);
            detaiVo.setTd008(""+roudedDecumal3);
        }
        return Result.success(vo);
    }

    @ApiOperation(value = "生产车间看板")
    @GetMapping("/selectWorkShopDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "day" , value = "需要查询的天数(0:当天，3:3天内，7:一周内)")
    })
    public Result<CustomerWorkShopDetailVo> selectWorkShopDetail(
            @RequestParam(value = "day",required = false,defaultValue = "0") Integer day
    ){
        return Result.success(new CustomerWorkShopDetailVo().setValue(customerService.selectWorkShopDetail(day)));
    }

    @ApiOperation(value = "在制任务进度表")
    @GetMapping("/selectTaskprogress")
    public Result<List<Taskprogressvo>> taskprogress(){
        return Result.success(customerService.taskprogress());
    }

}
